package com.adminx.controller.admin;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.adminx.entity.Code;
import com.adminx.pojo.CodePojo;
import com.adminx.pojo.NavigationMenuPojo;
import com.adminx.service.CodeService;
import com.adminx.service.ResourceService;
import com.adminx.util.Constant;
import com.adminx.util.PageResults;

@Controller
@RequestMapping("code")
public class CodeController {
	
	@Autowired
	private CodeService codeService;
	
	@Autowired
	private ResourceService resourceService;
	
	/**
	 * 初始化CODE页面
	 * @param map
	 * @return 
	 */
	@RequestMapping(value = "/init", method = RequestMethod.GET) 
	public String init(ModelMap map){
		NavigationMenuPojo navigationMenuPojo = this.resourceService.queryNavigation("/code/init.html");
		map.put("navigationMenuPojo", navigationMenuPojo);
		map.put("url", "/code/init.html");
		return "admin/code/code";
	}
	
	/**
	 * 分页查找
	 */
	@ResponseBody
	@RequestMapping(value = "/query", method = RequestMethod.GET) 
	public Map<String, Object> queryValueCode(Integer pageIndex, Integer pageSize, String searchText){
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			PageResults<CodePojo> result = this.codeService.queryValueCode(pageIndex, pageSize, searchText);
			map.put("result", result);
			map.put("operateMsg", Constant.SUCCESS_CODE);
		} catch (Exception e) {
			e.printStackTrace();
			map.put("operateMsg", Constant.FAIL_CODE);
		}
		return map;
	}
	
	/**
	 * 分页查找子集
	 */
	@ResponseBody
	@RequestMapping(value = "/queryValueDetail", method = RequestMethod.GET) 
	public Map<String, Object> queryValueDetail(Integer pageIndex, Integer pageSize, Long codeId){
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			PageResults<CodePojo> result = this.codeService.queryValueDetail(pageIndex, pageSize, codeId);
			map.put("result", result);
			map.put("operateMsg", Constant.SUCCESS_CODE);
		} catch (Exception e) {
			e.printStackTrace();
			map.put("operateMsg", Constant.FAIL_CODE);
		}
		return map;
	}
	
	/**
	 * 新增/修改值集
	 * @param code
	 */
	@RequestMapping(value = "/save") 
	@ResponseBody
	public Map<String, Object> save(Code code){
		Map<String, Object> map = new HashMap<String, Object>();
		try{
			this.codeService.save(code);
			int parentId = code.getParentId();
			if(parentId != 0){
				map.put("parentId", parentId);
			}
			map.put("operateMsg", Constant.SUCCESS_CODE);
		}catch(Exception e){
			e.printStackTrace();
			map.put("operateMsg", Constant.FAIL_CODE);
		}
		return map;
	}
	
	/**
	 * 通过ID查询code
	 */
	@ResponseBody
	@RequestMapping(value = "/info", method = RequestMethod.GET) 
	public Map<String, Object> info(Long id){
		Map<String, Object> map = new HashMap<String, Object>();
		try{
			Code code = this.codeService.info(id);
			map.put("code", code);
			map.put("operateMsg", Constant.SUCCESS_CODE);
		}catch(Exception e){
			e.printStackTrace();
			map.put("operateMsg", Constant.FAIL_CODE);
		}
		return map;
	}
	
	/**
	 * 禁用code
	 */
	@ResponseBody
	@RequestMapping(value = "/ban", method = RequestMethod.GET) 
	public Map<String, Object> ban(Long id){
		Map<String, Object> map = new HashMap<String, Object>();
		try{
			this.codeService.ban(id);
			map.put("operateMsg", Constant.SUCCESS_CODE);
		}catch(Exception e){
			e.printStackTrace();
			map.put("operateMsg", Constant.FAIL_CODE);
		}
		return map;
	}
	
	/**
	 * 禁用valueDetail
	 */
	@ResponseBody
	@RequestMapping(value = "/banValueDetail", method = RequestMethod.GET) 
	public Map<String, Object> banValueDetail(Long id){
		Map<String, Object> map = new HashMap<String, Object>();
		try{
			this.codeService.ban(id);
			int parentId = this.codeService.info(id).getParentId();
			map.put("parentId", parentId);
			map.put("operateMsg", Constant.SUCCESS_CODE);
		}catch(Exception e){
			e.printStackTrace();
			map.put("operateMsg", Constant.FAIL_CODE);
		}
		return map;
	}
}
